package com.errol.batchprocessor.employeeaddition.advisorimpl.common;

import com.baomidou.mybatisplus.mapper.Condition;
import com.renjia.entity.CoreUserBank;
import com.renjia.enums.EasyImportTypeEnum;
import com.renjia.pojo.AutoCompleteFlagParser;
import com.renjia.service.module.employeeaddition.advisorimpl.common.payload.AbstractEmployeeBatchPayload;
import com.renjia.service.module.employeeaddition.advisorimpl.common.payload.AbstractEmployeePiecePayload;
import com.renjia.service.module.employeeaddition.advisorimpl.common.payload.AbstractEmployeeSinglePayload;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;

import java.util.List;

/**
 * 检查是否开启自动补全，及补全人员信息
 *
 * @author zhang xiao bin
 * @version v1.0
 * @date 2021/06/07
 * @since v5.2.6
 **/
public interface EmployeeAutoCompleteModifier<BE, SE, BP extends AbstractEmployeeBatchPayload<BE, SE>, PP extends AbstractEmployeePiecePayload<SE, SP>, SP extends AbstractEmployeeSinglePayload<SE>,
        IMPL extends AbstractEmployeeAdvisorImpl<BE, SE, BP, PP, SP>> {

    /**
     * 导入入口的 advisor 分别实现，用来补全 SE 中银行卡和手机号
     * 已综合考虑提交信息和补全信息，可以直接覆盖
     *
     * @param singlePayload          SP
     * @param bankCard               提交的银行卡或补全的银行卡
     * @param mobile                 提交手机号或补全的手机号
     * @param autoCompleteFlagParser 自动补全标识位解析器
     */
    void autoCompleteSingleEntity(@NonNull SP singlePayload, @NonNull String bankCard, @NonNull String mobile, @NonNull AutoCompleteFlagParser autoCompleteFlagParser);

    /**
     * 导入人员是调用
     * 检查是否开启自动补全，检查银行卡和手机号是否未提交
     * 未提交，且可补全有效信息，才会触发补全
     *
     * @param singlePayload       SP
     * @param piecePayload        PP
     * @param batchPayload        BP
     * @param employeeAdvisorImpl 人员编辑实现
     * @return true always
     */
    default void checkAutoComplete(@NonNull SP singlePayload, @NonNull PP piecePayload, @NonNull BP batchPayload, @NonNull IMPL employeeAdvisorImpl) {
        if (singlePayload.getRawBankCard() != null && singlePayload.getMobile() != null) {
            // 都填写了不用补全
            return;
        }
        if (EasyImportTypeEnum.MOBILE_AND_BANKCARD.getCode().equals(batchPayload.getCoreCustomerChannelMerchant().getAllowEasyImport())) {
            if (singlePayload.getCoreEmployee() == null) {
                // 身份证未找到用户，且手机号上传了，则尝试通过手机号查找用户
                if (StringUtils.isNotEmpty(singlePayload.getRawMobile())) {
                    if (!employeeAdvisorImpl.findEmployeeByMobile(singlePayload, batchPayload)) {
                        return;
                    }
                }
                if (singlePayload.getCoreEmployee() == null) {
                    // 未找到用户，无法补全
                    return;
                }
            }
            // 尝试补全
            boolean autoCompleteBankCard = false;
            boolean autoCompleteMobile = false;
            if (StringUtils.isEmpty(singlePayload.getRawBankCard())) {
                // 银行卡未上传，预加载不会查找银行卡；这里多数情况可以从 employeeCustomer 取得银行卡号，但验证时还是要查一次银行卡表
                if (singlePayload.getCoreEmployeeCustomer() != null && StringUtils.isNotEmpty(singlePayload.getCoreEmployeeCustomer().getBankCard())) {
                    singlePayload.setAutoCompleteBankCard(singlePayload.getCoreEmployeeCustomer().getBankCard());
                    autoCompleteBankCard = true;
                } else {
                    // 查找用户最新的银行卡
                    List<CoreUserBank> coreUserBanks = employeeAdvisorImpl.coreUserBankMapper.selectPage(new RowBounds(0, 1),
                            Condition.<CoreUserBank>wrapper().eq("user_id", singlePayload.getCoreEmployee().getId())
                                    .orderBy("update_time", false));
                    if (!coreUserBanks.isEmpty()) {
                        singlePayload.setCoreUserBank(coreUserBanks.get(0));
                        if (StringUtils.isNotEmpty(singlePayload.getCoreUserBank().getCardNo())) {
                            singlePayload.setAutoCompleteBankCard(singlePayload.getCoreUserBank().getCardNo());
                            autoCompleteBankCard = true;
                        }
                    }
                }
            }
            if (StringUtils.isEmpty(singlePayload.getRawMobile()) && StringUtils.isNotEmpty(singlePayload.getCoreEmployee().getMobile())) {
                singlePayload.setAutoCompleteMobile(singlePayload.getCoreEmployee().getMobile());
                autoCompleteMobile = true;
            }
            if (autoCompleteBankCard || autoCompleteMobile) {
                // 有一个补全就要触发，补全失败的使用上传数据
                this.autoCompleteSingleEntity(singlePayload, singlePayload.getBankCard(), singlePayload.getMobile(), new AutoCompleteFlagParser(autoCompleteBankCard, autoCompleteMobile));
            }
        }
    }

    /**
     * 编辑人员时调用
     * 如果导入时自动补全了，且编辑时没有更新，则保留自动补全状态
     * 如果编辑时更新了，则不再算自动补全
     *
     * @param singlePayload SP
     * @param oldParser     编辑前的自动补全状态
     * @param oldBankCard   编辑提交的银行卡
     * @param oldMobile     编辑提交的手机号
     * @return 编辑后的自动补全状态
     */
    default AutoCompleteFlagParser checkAutoComplete(@NonNull SP singlePayload, @NonNull AutoCompleteFlagParser oldParser, @Nullable String oldBankCard, @Nullable String oldMobile) {
        return new AutoCompleteFlagParser(
                StringUtils.equals(singlePayload.getBankCard(), oldBankCard) && oldParser.isAutoCompleteBankCard(),
                StringUtils.equals(singlePayload.getMobile(), oldMobile) && oldParser.isAutoCompleteMobile()
        );
    }
}
